package com.example.cart;

import static com.example.cart.utils.GoodsDetailUtil.renderGoodsUnit;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.cart.adapter.GoodsListAdapter;
import com.example.cart.dto.Goods;
import com.example.cart.repository.GoodsDao;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private GoodsDao goodsDao;

    private TextView cartCountView;
    private GoodsListAdapter goodsListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        goodsDao = CartApplication.get().getGoodsDataBase().goodsDao();

        GridView goodsListView = findViewById(R.id.goodsList);

        findViewById(R.id.goToCart).setOnClickListener(view->{

            startActivity(new Intent(this,CartActivity.class));

        });

        findViewById(R.id.goBack).setOnClickListener(view->{
            finish();
        });

        cartCountView = findViewById(R.id.cartCount);


        goodsListAdapter = new GoodsListAdapter(this);
        goodsListView.setAdapter(goodsListAdapter);
        loadData();
    }


    private void loadData() {


        Log.d("MainActivity","start 事件 执行");
        //goodsListView.removeAllViews();

        List<Goods> goods = goodsDao.page(0,10);


        goodsListAdapter.update(goods);


        /*
        // 获取屏幕的宽度
        int widthPixels = getResources().getDisplayMetrics().widthPixels;

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(widthPixels/2, ViewGroup.LayoutParams.WRAP_CONTENT);

        for (Goods good : goods) {

            View goodsItemView = LayoutInflater.from(this).inflate(R.layout.activity_goods_unit, null);

            renderGoodsUnit(good, goodsItemView);

            goodsItemView.setOnClickListener(view->{
                Intent intent = new Intent(this, GoodsDetailActivity.class);
                intent.putExtra("goods_id",good.getId());
                startActivity(intent);
            });



            goodsListView.addView(goodsItemView,layoutParams);
            //goodsListView.setRowCount(goods.size()/goodsListView.getColumnCount());
        }
*/
    }

    @Override
    protected void onResume() {
        super.onResume();


        cartCountView.setText(String.valueOf(CartApplication.get().getCartCount()));
    }
}